import { defineStore } from 'pinia'

export const useAppStore = defineStore('useAppStore', {
  state: () => ({
    demo: 0,
    // 窗口高度
    windowHeight: 0,
    windowWidth: 0,
    // 当前菜单激活项
    currentTabActive: null,
    //标签页存放
    tabPages: [],
    //定位当前标签页的激活位置
    tabValue: null,
    //标签页是否显示关闭按钮(只剩一个标签页不显示关闭按钮)
    closable: null,
    // 账号密码登录的用户信息
    userInfo: {},
    // 当前选中角色
    orgInfo: {},
    // 当前机构列表
    orgInfoList: [],
    // 系统菜单
    menus: [
      {
        "id": "b645bfa6520d431ca2d2db1d4f57ff41",
        "code": "xxly",
        "name": "新兴领域",
        "isLeaf": "0",
        "parentId": "e8cfc31b5400460ab5ea7a25c570ea7e",
        "children": [
          {
            "id": "7b100b5f32f7411080c1113cb4e0abb5",
            "code": "xxly-lqsx",
            "name": "两企三新管理信息",
            "url": "log",
            "icon": "icon-governance",
            "parentId": "b645bfa6520d431ca2d2db1d4f57ff41",
            "children": [
              {
                "id": "8b6a3f2f8dc5483ebb8376745115b681",
                "code": "LogManagement",
                "name": "“三有”非公企业法人单位党建工作台账",
                "url": "LogManagement",
                "isLeaf": "1",
                "parentId": "7b100b5f32f7411080c1113cb4e0abb5"
              },
              {
                "id": "8b6a3f2f8dc5483ebb8376745115b682",
                "code": "LogManagement",
                "name": "“三有”非公企业法人单位党建工作汇总",
                "url": "LogManagement",
                "isLeaf": "1",
                "parentId": "7b100b5f32f7411080c1113cb4e0abb5"
              },
              {
                "id": "8b6a3f2f8dc5483ebb8376745115b683",
                "code": "LogManagement",
                "name": "社会组织党建工作台账",
                "url": "LogManagement",
                "isLeaf": "1",
                "parentId": "7b100b5f32f7411080c1113cb4e0abb5"
              },
              {
                "id": "8b6a3f2f8dc5483ebb8376745115b684",
                "code": "LogManagement",
                "name": "全国性(省级)社会组织党建信息汇总",
                "url": "LogManagement",
                "isLeaf": "1",
                "parentId": "7b100b5f32f7411080c1113cb4e0abb5"
              },
              {
                "id": "8b6a3f2f8dc5483ebb8376745115b685",
                "code": "LogManagement",
                "name": "重点商务楼宇、商圈市场、国家备案众创 空间党建工作汇总",
                "url": "LogManagement",
                "isLeaf": "1",
                "parentId": "7b100b5f32f7411080c1113cb4e0abb5"
              },
              {
                "id": "8b6a3f2f8dc5483ebb8376745115b686",
                "code": "LogManagement",
                "name": "国家级园区党建工作情况汇总",
                "url": "LogManagement",
                "isLeaf": "1",
                "parentId": "7b100b5f32f7411080c1113cb4e0abb5"
              },
              {
                "id": "8b6a3f2f8dc5483ebb8376745115b687",
                "code": "LogManagement",
                "name": "省级园区党建工作情况汇总",
                "url": "LogManagement",
                "isLeaf": "1",
                "parentId": "7b100b5f32f7411080c1113cb4e0abb5"
              },
            ]
          }
        ]
      }
    ],
  }),
  getters: {
    // 获取屏幕高度
    getWindowHeight() {
      return this.windowHeight
    },
    // 获取屏幕宽度
    getWindowWidth() {
      return this.windowWidth
    },
    // 获取当前高亮标签页
    getCurrentTabActive() {
      return this.currentTabActive
    },
    // 获取当前存在的标签页
    getTabPages() {
      return this.tabPages
    },
    // 获取指定标签页
    getTabValue() {
      return this.tabValue
    },
    // 获取标签页关闭按钮
    getClosable() {
      return this.closable
    },
    // 获取标签页关闭按钮
    getMenus() {
      return this.menus
    }
  },
  actions: {
    setWindowHeight(windowHeight) {
      this.windowHeight = windowHeight
    },
    setWindowWidth(windowWidth) {
      this.windowWidth = windowWidth
    },
    addTabPages(tabPage) {
      //  新增的标签页存储
      let exist = false // 标签页是否存在标识
      this.tabPages.map((tabP) => {
        // 重复点击一个标签不增加标签页
        if (tabP.component == tabPage.component) {
          // this.tabPages.splice()
          this.tabValue = tabPage.component
          exist = true
        }
      })
      if (!exist) {
        this.tabPages.push(tabPage)
        this.tabValue = tabPage.component
        if (this.tabPages.length < 2) {
          this.closable = false
        } else {
          this.closable = true
        }
      }
    },
    addTabPagesRepeat(tabPage) {
      //  新增的标签页存储
      this.tabPages.push(tabPage)
      this.tabValue = tabPage.component
      if (this.tabPages.length < 2) {
        this.closable = false
      } else {
        this.closable = true
      }
    },
    removeTabPages(component) {
      //  移除的标签页并返回前一个或者后一个标签页状态
      let tabs = this.tabPages
      let value = this.tabValue
      if (value == component) {
        tabs.forEach((tab, index) => {
          if (tab.component == component) {
            let nextTab = tabs[index + 1] || tabs[index - 1]
            if (nextTab) {
              value = nextTab.component
            }
          }
        })
      }
      this.tabValue = value
      this.tabPages = tabs.filter((tab) => tab.component !== component)
      if (this.tabPages.length < 2) {
        this.closable = false
      } else {
        this.closable = true
      }
    },
    setTabValue(tabValue) {
      this.tabValue = tabValue
    },
    setTabPages(tabPages) {
      this.tabPages = tabPages
    },
    setClosable(closable) {
      this.closable = closable
    },
    logout() {
      sessionStorage.removeItem('token')
    }
  },
  persist: {
    key: 'appStore',
    storage: sessionStorage,
    debug: true
  },
})
